﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class Templates_RootMasterPage : MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlLink htmlLink;
        foreach (Control ctrl in this.head.Controls)
        {
            if (ctrl is HtmlLink)
            {
                htmlLink = (HtmlLink)ctrl;
                htmlLink.Href = htmlLink.Href.Replace("%CurrentDesign%", CrossFactory.SiteNote.Components.Utils.GetCurrentDesign());
            }
        }
    }

    protected override void OnInit(EventArgs e)
    {
        LoadResources();
        base.OnInit(e);
    }

    public string LanguageEntry
    {
        get
        {
            try
            {
                if (this.Page is CrossFactory.SiteNote.Hierarchy.SiteNotePage)
                {
                    if (((CrossFactory.SiteNote.Hierarchy.SiteNotePage)this.Page).SiteNotePageInfo.VirtualPath.IndexOf(".off.") != -1)
                    {
                        return CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.RootPage + "/" + CrossFactory.SiteNote.Components.Language._GetLanguageUser() + ".off.aspx";
                    }
                    else
                    {
                        return CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.RootPage + "/" + CrossFactory.SiteNote.Components.Language._GetLanguageUser() + ".aspx";
                    }
                }
            }
            catch (Exception exc) { }
            return "";
        }
    }

    private static void LoadResources()
    {
        try
        {
            try
            {
                if (CrossFactory.SiteNote.Components.Language._GetLanguageUser() != null)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(CrossFactory.SiteNote.Components.Language._GetLanguageUser(), true);
                }
                else
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(CrossFactory.SiteNote.Components.Language._GetPageLanguage(HttpContext.Current.Request.RawUrl), true);
                }
            }
            catch (Exception ex4)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US", true);
            }
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
        }
        catch (Exception ex5)
        {
            string test = "";
        }
    }


    public static string GetLanguage()
    {
        try
        {
            return CrossFactory.SiteNote.Components.Language._GetLanguageUser().ToLower();
        }
        catch (Exception ex)
        {
            return "en-US";
        }
    }

    public static string GetShortLanguage()
    {
        try
        {
            return CrossFactory.SiteNote.Components.Language._GetLanguageUser().ToLower().Substring(0, 2);
        }
        catch (Exception ex)
        {
            return "en-US";
        }
    }

    public static string GetShortLanguageUpper()
    {
        try
        {
            return CrossFactory.SiteNote.Components.Language._GetLanguageUser().ToUpper().Substring(0, 2);
        }
        catch (Exception ex)
        {
            return "en-US";
        }
    }

    public void ChangeLanguage(string language)
    {
        string oldLanguage;
        string newUrl = "";
        string virtualUrl;
        //int startPos;
        CrossFactory.SiteNote.Hierarchy.SiteNotePage siteNotePage;

        oldLanguage = CrossFactory.SiteNote.Components.Language._GetLanguageUser();
        CrossFactory.SiteNote.Components.Language._SetLanguageUser(language);

        try
        {
            newUrl = HttpContext.Current.Request.UrlReferrer.AbsoluteUri.ToLower();
        }
        catch (Exception ex) { }

        if (newUrl.IndexOf(oldLanguage.ToLower()) != -1)
        {
            newUrl = newUrl.Replace(oldLanguage.ToLower(), language.ToLower());

            //startPos = newUrl.ToLower().IndexOf(CrossFactory.SiteNote.Components.Utils._ApplicationPath);
            //virtualUrl = newUrl.Substring(startPos, newUrl.Length - startPos);
            virtualUrl = HttpContext.Current.Request.RawUrl.ToLower().Replace(oldLanguage.ToLower(), language.ToLower());
            siteNotePage = CrossFactory.SiteNote.Components.Settings._settings.HierarchyProvider.GetPageWithPath(virtualUrl);
            if (siteNotePage != null)
            {
                HttpContext.Current.Response.Redirect(newUrl);
            }
            else
            {
                if (CrossFactory.SiteNote.Components.Utils._InAdminMode)
                {
                    HttpContext.Current.Response.Redirect(CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.RootPage + "/" + language.ToLower() + ".off.aspx");
                }
                else
                {
                    HttpContext.Current.Response.Redirect(CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.RootPage + "/" + language.ToLower() + ".aspx");
                }
            }
        }
        else
        {
            if (CrossFactory.SiteNote.Components.Utils._InAdminMode)
            {
                HttpContext.Current.Response.Redirect(CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.RootPage + "/" + language.ToLower() + ".off.aspx");
            }
            else
            {
                HttpContext.Current.Response.Redirect(CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.RootPage + "/" + language.ToLower() + ".aspx");
            }
        }
    }

    public void frlink_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        ChangeLanguage("fr-BE");
    }

    public void nllink_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        ChangeLanguage("nl-BE");
    }

    public void enlink_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        ChangeLanguage("en-US");
    }


}
